<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 汪利东,2016-04-12 10:33
// +----------------------------------------------------------------------

namespace Admin\Controller;


class PayController extends BaseController
{

    public function index()
    {
        $mapstr='';
        $pay_id = I('get.pay_id');
        if (!empty($pay_id)) {
            $mapstr.='a.pay_id='.$pay_id;
        }
        $keywords = I('get.keywords');
        if (!empty($keywords)) {
            if($mapstr!=''){
                $mapstr.=' and ';
            }
            $mapstr.='(a.pay_id like "%'.$keywords.'%" or b.card_id="'.$keywords.'")';
        }
        $interval_time = I('get.interval_time');
        if (!empty($interval_time)) {
            $time=explode('~',$interval_time);
            $time[0]=$time[0].' 00:00:00';
            $time[1]=$time[1].' 23:59:59';
            if($mapstr!=''){
                $mapstr.=' and ';
            }
            $mapstr.=' a.create_time >"'.$time[0].'" and a.create_time < "'.$time[1].'"';
        }

        $count = D('Pay')->alias('a')->where($mapstr)->join('card b on a.user_id=b.user_id','left')->count();// 查询满足要求的总记录数
        $Page = new  \Admin\Common\Page($count, C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $where['keywords'] = $keywords;
        foreach ($where as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $show = $Page->show();// 分页显示输出
        $this->assign('page', $show);// 赋值分页输出

        $data = D('Pay')->alias('a')
            ->where($mapstr)
            ->join('card b on a.user_id=b.user_id','left')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('a.pay_id desc')->field('a.*,b.card_id')
            ->select();
        if(I("get.execl")==1){
            $data = D('Pay')->alias('a')
                ->where($mapstr)
                ->join('card b on a.user_id=b.user_id','left')
                ->order('a.pay_id desc')->field('a.*,b.card_id')
                ->select();
            $data = $this->format($data);
            $file = excel_export($data);
            ouput_Browser($file);
        }else{
            $this->data = $data;
            $this->display();
        }
    }

    private function format($data)
    {
        return array_map(function ($item) {
            $data['支付ID'] = $item['pay_id'];
            $data['用户ID'] = $item['user_id'];
            $statusList = ['-1'=>'取消','0'=>'下单','1'=>'支付成功','2'=>'支付失败'];
            $data['支付状态'] = $statusList[$item['status']];
            $typeList = ['未知','借书','购书','会员充值','活动','破损'];
            $data['类型'] = $typeList[$item['type']];
            $data['付款方式'] = $item['way'];
            $data['描述'] = $item['summary'];
            $data['应付款'] = $item['money'];
            $data['实收'] = $item['money_real'];
            $data['找零'] = $item['money_zero'];
            $data['创建时间'] = $item['create_time'];
            $data['卡号'] = $item['card_id'];
            return $data;
        },$data);
    }
    function exportAll(){
        $p = I('get.p/d',0);

        $lenght = $this->exportLenght;
        $d=D('Pay')->alias('a')
            ->join('card b on a.user_id=b.user_id','left')
            ->field('a.*,b.card_id')
            ->limit(($p-1)*$lenght,$p*$lenght)
            ->select();

//        var_dump($d);exit;

        $file=excel_export($d);
        ouput_Browser($file);
    }

    function getWay(){
        $d=M('Pay_way')->select();
        if($d){

            $j=[
                'status'=>'success',
                'data'=>$d
            ];
        }else{
            $j=[
                'status'=>'error',
                'data'=>'查询错误，支付方式不存在'
            ];
        }
        echo json_encode($j);
    }
}